//
//  LJLRadioPageLocalVC.m
//  聆听FM
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Dark. All rights reserved.
//
#import "LJLRadioPageModel.h"
#import <MJRefresh.h>
#import "LJLRadioPageLocalVC.h"
#import "LJLRPLocalTVC.h"
#import "ZCSPlayMusicViewController.h"
#import "CBStoreHouseRefreshControl.h"
@interface LJLRadioPageLocalVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *localTV;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger pageNum;

@property (nonatomic, strong) CBStoreHouseRefreshControl *storeHouseRefreshControl;
@end

@implementation LJLRadioPageLocalVC

- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.type == 2) {
        self.navigationItem.title = @"本地台";
    }
    else if (self.type == 1)
    {
        self.navigationItem.title = @"国家台";
    }
    else
    {
        self.navigationItem.title = @"网络台";
    }
    
    self.view.backgroundColor = [UIColor grayColor];
    self.dataSource = [NSMutableArray array];
    self.navigationController.navigationBar.translucent = YES;
    [self createView];
    self.pageNum = 1;
    [self createData:self.provinceCode radioType:self.type WithPage:self.pageNum];
   
}


- (void)createView
{
    if (self.style == 1) {
        self.localTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    }
    else
    {
        self.localTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    }
    self.localTV.backgroundColor = [UIColor whiteColor];
    self.localTV.delegate = self;
    self.localTV.dataSource = self;
    if (self.style == 1) {
        UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 120 * FITHEIGHT)];
        self.localTV.tableFooterView = foot;
    }
    else
    {
        UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 80 * FITHEIGHT)];
        self.localTV.tableFooterView = foot;
    }
    
    [self.view addSubview:_localTV];
    
    [self.localTV registerClass:[LJLRPLocalTVC class] forCellReuseIdentifier:@"LJLRPlocalTVC"];
    self.storeHouseRefreshControl = [CBStoreHouseRefreshControl attachToScrollView:self.localTV target:self refreshAction:@selector(refreshTriggered:) plist:@"LTFM" color:[UIColor blackColor] lineWidth:3 dropHeight:80 scale:1.2 horizontalRandomness:300 reverseLoadingAnimation:NO internalAnimationFactor:0.7];
    
    //加载
    self.localTV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self getMoreData];
    }];
}

- (void)getMoreData
{
    self.pageNum++;
    [self createData:self.provinceCode radioType:self.type WithPage:self.pageNum];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_style == 1) {
       [self.storeHouseRefreshControl scrollViewDidScrollWithType:0];
    }
    else
    {
    [self.storeHouseRefreshControl scrollViewDidScrollWithType:1];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self.storeHouseRefreshControl scrollViewDidEndDragging];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)refreshTriggered:(id)sender
{
    
    [self performSelector:@selector(finishRefreshControl) withObject:nil afterDelay:2 inModes:@[NSRunLoopCommonModes]];
}
- (void)finishRefreshControl
{
    [self.dataSource removeAllObjects];
    [self.localTV reloadData];
    self.pageNum = 1;
    [self createData:self.provinceCode radioType:self.type WithPage:self.pageNum];
}

- (void)createData:(NSInteger)provinceCode radioType:(NSInteger)type WithPage:(NSInteger)page
{
    NSMutableString *urlString = [NSMutableString string];
    if (type == 2) {
        urlString = [NSMutableString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&pageSize=20&provinceCode=%ld&radioType=2", page,provinceCode];
    }
    else
    {
        urlString = [NSMutableString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getRadiosListByType?pageNum=%ld&pageSize=20&radioType=%ld", page, type];
    }
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:0 Success:^(id result) {
        NSArray *array = [result objectForKey:@"result"];
        for (NSDictionary *dic in array) {
            LJLRadioPageModel *model = [[LJLRadioPageModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        
        }
        if (self.dataSource.count) {
            [self.localTV reloadData];
            [self.storeHouseRefreshControl finishingLoading];
            [self.localTV.mj_footer endRefreshing];
        }
    } Failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataSource.count != 0) {
        return self.dataSource.count;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90 *FITHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource.count != 0) {
        LJLRPLocalTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLRPlocalTVC"];
        cell.model = [self.dataSource objectAtIndex:indexPath.row];
        return cell;
    }
        LJLRPLocalTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"LJLRPlocalTVC"];
        return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
    
    NSMutableArray *tempArray = [NSMutableArray arrayWithObjects:self.dataSource[indexPath.row], nil];
    play.isRadio = YES;
    play.dataArray = tempArray;
    play.musicIndex = 0;
    [self presentViewController:play animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
